#include <iostream>
#include <memory>
#include <string>
#include <functional>
#include<unistd.h>

#include "PackageParse.hpp"
#include "Reactor.hpp"
#include "Connection.hpp"
#include "Listener.hpp"
#include "HandlerConnection.hpp"

using namespace socket_ns;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage:\n\t" << argv[0] << " port" << std::endl;
        return 0;
    }

    uint16_t serverport = std::stoi(argv[1]);
    std::unique_ptr<Reactor> r = std::make_unique<Reactor>();
    HandlerConnection hc(PackageParse::Parse);
    // 单独创建一个listener的目的是解耦，将listener和其他的socket平等化，不在Reactor中做单独处理，对于accept方法，则是通过回调function的方式实现多态
    Listener listen(serverport, hc);
    // 添加listen的socket到reactor中。并将事件设为IN、ET模式，绑定accpet方法
    r->AddConnection(listen.SockFd(), EPOLLIN | EPOLLET, std::bind(&Listener::Accept, &listen, std::placeholders::_1), nullptr, nullptr);
    r->Dispatcher();

    return 0;
}